package init

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"service/basic/config"
	"sync"
)

func InitsMysql() {
	var err error
	var once sync.Once //设置单例模式
	mysqlCong := config.AppCong.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqlCong.User, mysqlCong.Password, mysqlCong.Host, mysqlCong.Port, mysqlCong.Database)
	once.Do(func() {
		config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
		if err != nil {
			panic(err)
		}
		log.Println("mysql init success")
	})
}
